package sopt.dev.activity;

import java.util.List;

import sopt.dev.R;
import sopt.dev.data.CityDetailAdapter;
import sopt.dev.data.CityVO;
import sopt.dev.data.GagebuDataModel;
import sopt.dev.data.ItemVO;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class CityDetailActivity extends Activity implements OnClickListener {
	private TextView tvCityName, tvInDate, tvOutDate, tvCitySum;
	private Button btnAdd;

	private List<String> mGroupList;
	private List<List<ItemVO>> mChildList;

	private ExpandableListView mListView;

	private CityDetailAdapter adapter;
	private CityVO<ItemVO> data;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.city_detail_activity);

		mListView = (ExpandableListView) findViewById(R.id.city_detail_listview);

		// title 변수
		tvCityName = (TextView) findViewById(R.id.city_detail_tv_cityName);
		tvInDate = (TextView) findViewById(R.id.city_detail_tv_indate);
		tvOutDate = (TextView) findViewById(R.id.city_detail_tv_outdate);
		tvCitySum = (TextView) findViewById(R.id.city_detail_tv_citysum);

		btnAdd = (Button) findViewById(R.id.city_detail_btn_add);
		btnAdd.setOnClickListener(this);

		// title 설정
		data = getDataFromIntent();

		try {
			String cityName = data.getCityName();
			String citySum = Double.toString(GagebuDataModel.getInstance().getSumofCity(cityName));

			// group
			mGroupList = GagebuDataModel.getInstance().getDateListInCity(
					cityName);

			// child
			mChildList = GagebuDataModel.getInstance()
					.getItemDataGroupedByDate(cityName);

			adapter = new CityDetailAdapter(this, mGroupList, mChildList);
			mListView.setAdapter(adapter);

			// 이미지뷰에 사진 경로 추가
			tvCityName.setText(data.getCityName());
			tvInDate.setText((data.getInString() + " ~ "));
			tvOutDate.setText(data.getOutString());


			tvCitySum.setText(citySum);

			int groupCount = adapter.getGroupCount();

			for (int i = 0; i < groupCount; ++i) {
				mListView.expandGroup(i);
			}
		} catch(NullPointerException e) {
			e.printStackTrace();
		}

		// 차일드 클릭 했을 경우 이벤트
		mListView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {

				ItemVO item = mChildList.get(groupPosition).get(childPosition);

				Intent i = new Intent(CityDetailActivity.this,
						ExpenseClickedActivity.class);
				i.putExtra("item_data", item);
				startActivity(i);

				return false;
			}
		});

		mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> av, View v,
					int position, long id) {

				ItemVO item = (ItemVO) mListView.getItemAtPosition(position);

				Intent i = new Intent(CityDetailActivity.this,
						ExpenseAddActivity.class);

				i.putExtra("item_data", item);
				startActivity(i);

				return false;
			}
		});



	}

	private CityVO<ItemVO> getDataFromIntent() {
		Intent i = getIntent();
		return (CityVO<ItemVO>) i.getParcelableExtra("city_data");
	}





	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.city_detail_btn_add: {
			Intent i = new Intent(CityDetailActivity.this,
					ExpenseAddActivity.class);
			i.putExtra("city_data", data);
			startActivity(i);
			finish();
		}
		}
	}

}
